[XEND] Exposing the XendAPI module in the XMLRPCServer
authorAlastair Tse <atse@xensource.com>
Thu, 5 Oct 2006 16:29:19 +0000 (17:29 +0100)
committerAlastair Tse <atse@xensource.com>
Thu, 5 Oct 2006 16:29:19 +0000 (17:29 +0100)
Signed-off-by: Alastair Tse <atse@xensource.com>
tools/python/xen/xend/server/XMLRPCServer.py

index 8ff370ddbd3cfedd5f696aa1d6e88035ff2459fe..b366eec29ca43d555608a4c44784ea2760ad6e4b 100644 (file)
@@ -16,7 +16,7 @@
 # Copyright (C) 2006 XenSource Ltd.
 #============================================================================
 
-from types import ListType
+import types
 import xmlrpclib
 from xen.util.xmlrpclib2 import UnixXMLRPCServer, TCPXMLRPCServer
 
@@ -24,30 +24,31 @@ from xen.xend import XendDomain, XendDomainInfo, XendNode
 from xen.xend import XendLogging, XendDmesg
 from xen.xend.XendClient import XML_RPC_SOCKET
 from xen.xend.XendLogging import log
+from xen.xend.XendAPI import XendAPI
 from xen.xend.XendError import XendInvalidDomain
 
-def lookup(domid):
-    info = XendDomain.instance().domain_lookup_by_name_or_id(domid)
-    if not info:
-        raise XendInvalidDomain(str(domid))
-    return info
-
-def dispatch(domid, fn, args):
-    info = lookup(domid)
-    return getattr(info, fn)(*args)
-
 # vcpu_avail is a long and is not needed by the clients.  It's far easier
 # to just remove it then to try and marshal the long.
 def fixup_sxpr(sexpr):
     ret = []
     for k in sexpr:
-        if type(k) is ListType:
+        if type(k) in (types.ListType, types.TupleType):
             if len(k) != 2 or k[0] != 'vcpu_avail':
                 ret.append(fixup_sxpr(k))
         else:
             ret.append(k)
     return ret
 
+def lookup(domid):
+    info = XendDomain.instance().domain_lookup_nr(domid)
+    if not info:
+        raise XendInvalidDomain(str(domid))
+    return info
+
+def dispatch(domid, fn, args):
+    info = lookup(domid)
+    return getattr(info, fn)(*args)
+
 def domain(domid):
     info = lookup(domid)
     return fixup_sxpr(info.sxpr())
@@ -92,6 +93,7 @@ class XMLRPCServer:
         
         self.ready = False        
         self.running = True
+        self.xenapi = XendAPI()
         
     def run(self):
         if self.use_tcp:
@@ -100,6 +102,16 @@ class XMLRPCServer:
         else:
             self.server = UnixXMLRPCServer(self.path, logRequests = False)
 
+        # Register Xen API Functions
+        # -------------------------------------------------------------------
+        # exportable functions are ones that do not begin with '_'
+        # and has the 'api' attribute.
+        
+        for meth_name in dir(self.xenapi):
+            meth = getattr(self.xenapi, meth_name)
+            if meth_name[0] != '_' and callable(meth) and hasattr(meth, 'api'):
+                self.server.register_function(meth, getattr(meth, 'api'))
+                
         # Legacy deprecated xm xmlrpc api
         # --------------------------------------------------------------------